package com.cq.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cq.excelModel.ActivityKolGroupExcel;
import com.cq.excelModel.ActivityPlatformInfluencertypeGroupExcel;
import com.cq.mapper.ActivityKolGroupMapper;
import com.cq.pojo.ActivityKolGroup;
import com.cq.pojo.ActivityPlatformInfluencertypeGroup;
import com.cq.pojo.Alog;
import com.cq.query.PageResultQuery;
import com.cq.result.PageResult;
import com.cq.service.*;
import com.cq.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivityKolGroupServiceImpl implements ActivityKolGroupService {


    @Autowired
    private ActivityKolGroupMapper activityKolGroupMapper;
    @Autowired
    private ActivityGroupService activityGroupService;// 1
    @Autowired
    private ActivityPlatformGroupService activityPlatformGroupService;// 2
    @Autowired
    private ActivityPlatformContentGroupService activityPlatformContentGroupService;// 3
    @Autowired
    private ActivityPlatformContentDirectionGroupService activityPlatformContentDirectionGroupService;// 4
    @Autowired
    private ActivityPlatformInfluencergradeGroupService activityPlatformInfluencergradeGroupService;// 5
    @Autowired
    private ActivityPlatformInfluencertypeGroupService activityPlatformInfluencertypeGroupService;// 6



    @Override
    public PageResult<ActivityKolGroup> findActivityKolGroup(PageResultQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());

        List<ActivityKolGroup> list = activityKolGroupMapper.selectActivityKolGroup(query);

        PageInfo<ActivityKolGroup> pageInfo = new PageInfo<>(list);

        return new PageResult<>(pageInfo.getPageNum(), pageInfo.getPages(), pageInfo.getTotal(), list);
    }

    @Override
    public void activityKolGroupDownload(HttpServletResponse response, PageResultQuery query) throws IOException {
        PageResult<ActivityKolGroup> kolGroupPageResult = this.findActivityKolGroup(query);
        List<ActivityKolGroup> kolGroupList = kolGroupPageResult.getList();

        String fileName = System.currentTimeMillis() + "";

        List<ActivityKolGroupExcel> kolGroupExcels = kolGroupList.stream()
                .map(a -> {
                    ActivityKolGroupExcel excel = new ActivityKolGroupExcel();
                    BeanUtils.copyProperties(a, excel);
                    Alog alog = a.getAlog();
                    BeanUtils.copyProperties(alog, excel);
                    return excel;
                }).collect(Collectors.toList());

        EasyExcel.write(response.getOutputStream(), ActivityKolGroupExcel.class)
                .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                .sheet("分活动分媒介分帖子")
                .doWrite(kolGroupExcels);
    }

    @Override
    public void kolDownload(HttpServletResponse response, PageResultQuery query) throws IOException {

        String fileName = System.currentTimeMillis() + "";

        switch (query.getKolType()) {
            case 1:
                activityGroupService.activityGroupDownload(response, query.getCampaignName());
                return;
            case 2:
                activityPlatformGroupService.activityPlatformGroupDownload(response, query);
                return;
            case 3:
                activityPlatformContentGroupService.activityPlatformContentGroupDownload(response, query);
                return;
            case 4:
                activityPlatformContentDirectionGroupService.activityPlatformContentDirectionGroupDownload(response, query);
                return;
            case 5:
                activityPlatformInfluencergradeGroupService.activityPlatformInfluencergradeGroupDownload(response, query);
                return;
            case 6:
                PageResult<ActivityPlatformInfluencertypeGroup> influencertypeGroup = activityPlatformInfluencertypeGroupService.findActivityPlatformInfluencertypeGroup(query);
                List<ActivityPlatformInfluencertypeGroup> influencertypeGroupList = influencertypeGroup.getList();

                List<ActivityPlatformInfluencertypeGroupExcel> influencertypeGroupCollect = influencertypeGroupList.stream()
                        .map(a -> {
                            ActivityPlatformInfluencertypeGroupExcel excel = new ActivityPlatformInfluencertypeGroupExcel();
                            BeanUtils.copyProperties(a, excel);
                            Alog alog = a.getAlog();
                            BeanUtils.copyProperties(alog, excel);
                            return excel;
                        }).collect(Collectors.toList());

                EasyExcel.write(response.getOutputStream(), ActivityPlatformInfluencertypeGroupExcel.class)
                        .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                        .sheet("分媒介分达人类型")
                        .doWrite(influencertypeGroupCollect);
                return;
            case 7:
                PageResult<ActivityKolGroup> kolGroupPageResult = this.findActivityKolGroup(query);
                List<ActivityKolGroup> kolGroupList = kolGroupPageResult.getList();


                List<ActivityKolGroupExcel> kolGroupExcels = kolGroupList.stream()
                        .map(a -> {
                            ActivityKolGroupExcel excel = new ActivityKolGroupExcel();
                            BeanUtils.copyProperties(a, excel);
                            Alog alog = a.getAlog();
                            BeanUtils.copyProperties(alog, excel);
                            return excel;
                        }).collect(Collectors.toList());

                EasyExcel.write(response.getOutputStream(), ActivityKolGroupExcel.class)
                        .registerWriteHandler(ExcelUtil.getHorizontalCellStyleStrategy(response, fileName))
                        .sheet("分活动分媒介分帖子")
                        .doWrite(kolGroupExcels);
        }

    }
}
